package service

import (
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/pkg/mysql"
)

var SysRoleMenuService = &sysRoleMenuService{}

type sysRoleMenuService struct{}

// 删除角色与菜单关联
func (sysRoleMenuService) DeleteRoleMenu(roleIds string) {
	sql := "delete from sys_role_menu where role_id in ( " + roleIds + " )"
	err := mysql.MysqlDb().Exec(sql).Error
	if err != nil {
		panic(err.Error())
	}
}

// 通过角色ID删除角色和菜单关联
func (sysRoleMenuService) DeleteRoleMenuByRoleId(roleId string) {
	sql := "delete from sys_role_menu where role_id = " + roleId
	err := mysql.MysqlDb().Exec(sql).Error
	if err != nil {
		panic(err.Error())
	}
}

// 通过角色获取菜单
func (sysRoleMenuService) SelectMenuIdByRoleId(roleIds []int) []int {
	var menuIds []int
	var roleMenus []system.SysRoleMenu
	err := mysql.MysqlDb().Model(&system.SysRoleMenu{}).Select("menu_id").Where("role_id in ?", roleIds).Find(&roleMenus).Error
	if err != nil {
		return nil
	}
	for _, menu := range roleMenus {
		menuIds = append(menuIds, menu.MenuId)
	}
	return menuIds
}
